#pragma once

#include "ConnectionPointHelper.h"

#include <atlcom.h>

using namespace ATL;

template <class T>
class CProxy_IRegFreeComEvents : public IConnectionPointImpl<T, &__uuidof(_IRegFreeComEvents), CComDynamicUnkArray>
{

public:

	//[id(1), helpstring("method onSumAsyncDone")] HRESULT onSumAsyncDone([in] int a, [in] int b, [in] int result);
	void Fire_onSumAsyncDone(int a, int b, int result)
	{
		T* pT = static_cast<T*>(this);
		int nConnectionIndex;
		int nConnections = m_vec.GetSize();

		CComVariant varResult;
		CComVariant* pvars = new CComVariant[3];

		for (nConnectionIndex = 0; nConnectionIndex < nConnections; nConnectionIndex++)
		{
			pT->Lock();
			CComPtr<IUnknown> sp = m_vec.GetAt(nConnectionIndex);
			pT->Unlock();

			IDispatch* pDispatch = reinterpret_cast<IDispatch*>(sp.p);
			if (pDispatch != NULL)
			{
				VariantClear(&varResult);
				pvars[2] = a;
				pvars[1] = b;
				pvars[0] = result;
				DISPPARAMS disp = { pvars, NULL, 3, 0 };
				pDispatch->Invoke(0x1, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &disp, &varResult, NULL, NULL);
			}
		}

	}

};
